﻿App.define('alert', [], (function () {
	var alert = {}, $container = $('.alert-container'),
		patent = '<a onclick="return false;" href="javascript:void(0);" class="uk-alert-close uk-close"></a><p>{text}</p>',
		timer = 0;

	var info = document.createElement('div');
	info.classList.add('uk-alert');
	info.setAttribute('data-uk-alert', '{fade: false}');

	var warning = document.createElement('div');
	warning.classList.add('uk-alert');
	warning.classList.add('uk-alert-warning');
	warning.setAttribute('data-uk-alert', '{fade: false}');

	var success = document.createElement('div');
	success.classList.add('uk-alert');
	success.classList.add('uk-alert-success');
	success.setAttribute('data-uk-alert', '{fade: false}');

	var danger = document.createElement('div');
	danger.classList.add('uk-alert');
	danger.classList.add('uk-alert-danger');
	danger.setAttribute('data-uk-alert', '{fade: false}');

	alert.info = function (text) {
		info.innerHTML = patent.replace('{text}', text);
		show($(info).clone());
	};

	alert.warning = function (text) {
		warning.innerHTML = patent.replace('{text}', text);
		show($(warning).clone());
	};

	alert.success = function (text) {
		success.innerHTML = patent.replace('{text}', text);
		show($(success).clone());
	};

	alert.danger = function (text) {
		danger.innerHTML = patent.replace('{text}', text);
		show($(danger).clone());
	};

	function show($clone) {
		clearTimeout(timer);
		$container.html('');
		TweenMax.set($clone, { y: -10 });
		$clone.appendTo($container);
		$clone.find('.uk-alert-close').click(function (e) {
			e.stopImmediatePropagation();
			clear();
		});
		TweenMax.to($clone, 1, { y: 0, ease: Elastic.easeOut });
		timer = setTimeout(clear, 5000);
	}

	function clear() {
		clearTimeout(timer);
		var $dialog = $container.find('.uk-alert');
		TweenMax.to($dialog, 0.5, {scale: 0.5, opacity: 0, onComplete: function () {
				$container.html('');
			}
		});
	}

	return alert;
})());